package com.turing.post.report.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.AccSysBranchCardDaySett;
import com.turing.post.report.util.AccSysBranchCardDaySettDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "report.impl.AccSysBranchCardDaySettDaoImpl")
@Remote
@Local
public class AccSysBranchCardDaySettDaoImpl implements
		AccSysBranchCardDaySettDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 插入对象
	 * 
	 * @param asbcds
	 * @throws PCPSApplicationException
	 */
	public void insert(AccSysBranchCardDaySett asbcds)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, asbcds);
	}

	/**
	 * 根据主键查找
	 * 
	 * @param asbcds
	 * @return
	 */
	public AccSysBranchCardDaySett getAccSysBranchCardDaySettByKey(
			AccSysBranchCardDaySett asbcds) {
		return _em.find(AccSysBranchCardDaySett.class, asbcds);
	}

	/**
	 * 删除某发卡商某日所有的销售机构卡种类日结数据
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @param saleBranchNo
	 *            销售分支机构号
	 */
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeAllAccSysBranchCardDaySettByTranDate(
			String issueCompanyNo, Date tranDate, String saleBranchNo) {
		Query query = _em
				.createNativeQuery("delete from AccSysBranchCardDaySett t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2 and t.saleBranchNo = ?3");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.setParameter(3, saleBranchNo);
		query.executeUpdate();
	}

	/**
	 * 根据发卡商号、日期、销售机构号、卡种类检索小于该日期的最大日期
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @param saleBranchNo
	 *            销售分支机构号
	 * @param cardClassId
	 *            卡种类ID
	 * @return
	 */
	public Date getMaxTranDate(String issueCompanyNo, Date tranDate,
			String saleBranchNo, BigInteger cardClassicId) {
		Query query = _em
				.createNativeQuery("select Max(t.tranDate) from AccSysBranchCardDaySett t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate < ?2 and t.saleBranchNo = ?3 and t.cardClassicId = ?4");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.setParameter(3, saleBranchNo);
		query.setParameter(4, cardClassicId);

		return (Date) query.getSingleResult();
	}

	/**
	 * 根据发卡商号及日期检索所有销售机构卡种类日结数据列表
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param tranDate
	 *            帐务日期
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AccSysBranchCardDaySett> getAllAccSysBranchCardDaySettByTranDate(
			String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery(
						"select t.* from AccSysBranchCardDaySett t where t.issueCompanyNo = ?1 and t.tranDate = ?2",
						AccSysBranchCardDaySett.class);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		return query.getResultList();
	}
}